﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace A1
{
    class Player : Paddle
    {

        bool _gamepad; //Wther we're using a gamepad
        PlayerIndex _index; // The gamepad to check
        GamePadState _padState; // The state of the gamepad
        KeyboardState _keyState; // The state of the keyboard
        int _gameHeight; //The game height
        int _gameWidth; //The game width

        /// <summary>
        /// Reports whether this paddle is using a game pad
        /// </summary>       
        public bool UsingGamepad
        {
            get { return _gamepad; }
        }
        /// <summary>
        /// The gamepad this player is using
        /// </summary>
        public PlayerIndex Index
        {
            get { return _index; }
        }

        /// <summary>
        /// Create a new player
        /// </summary>
        /// <param name="gamepad">Whether we're using a gamepad</param>
        /// <param name="index">The controller to use</param>
        /// <param name="position">The position on the field</param>
        /// <param name="sprite">The sprite of the paddle</param>
        /// <param name="gameHeight">The game height</param>
        /// <param name="gameWidth">The game width</param>
        /// <param name="b">The ball</param>
        /// <param name="gameTime">The gametime object</param>
        public Player(bool gamepad, int index, Game1.Position position, Texture2D sprite, int gameHeight, int gameWidth, Ball b, GameTime gameTime)
            : base(sprite, b, position, gameHeight, gameWidth, gameTime)
        {
            _gamepad = gamepad;
            _gameWidth = gameWidth;
            _gameHeight = gameHeight;
            if (index < 0 || index > 3)
                _index = PlayerIndex.One;
            else
                _index = (PlayerIndex)index;

            switch (position)
            {
                case Game1.Position.Left:
                    rec = new Rectangle(30, (gameHeight / 2) - 35, 10, 70);
                    break;
                case Game1.Position.Top:
                    rec = new Rectangle((gameWidth / 2) - 35, 30, 70, 10);
                    break;
                case Game1.Position.Bottom:
                    rec = new Rectangle(gameWidth / 2 - 35, gameHeight - 30, 70, 10);
                    break;
                case Game1.Position.Right:
                    rec = new Rectangle(gameWidth - 30, (gameHeight / 2) - 35, 10, 70);
                    break;
                default:
                    rec = new Rectangle(30, (gameHeight / 2) - 35, 10, 70);
                    break;
            }

        }

        public override void Update(GameTime gameTime)
        {
            ApplyPower();
            PowerCountdown(gameTime);


            if (_gamepad)
            {
                _padState = GamePad.GetState(_index);
            }
            else
            {
                _keyState = Keyboard.GetState();
            }
            if (position == Game1.Position.Right || position == Game1.Position.Left)
            {
                if (_padState.IsButtonDown(Buttons.DPadDown) || _keyState.IsKeyDown(Keys.Down))
                {
                    rec.Y += 10;

                }
                else if (_padState.IsButtonDown(Buttons.DPadUp) || _keyState.IsKeyDown(Keys.Up))
                {
                    rec.Y -= 10;

                }
                rec.Y = (int)MathHelper.Clamp(rec.Y, 0, (_gameHeight - rec.Height));
                if (rec.Intersects(ball.Rec))
                {
                    ball.LastHit = this;
                    ball.SpeedX = ball.SpeedX * -1;
                    if (position == Game1.Position.Left)
                    {
                        if (ball.SpeedX < 16)
                            ball.SpeedX++;

                    }
                    else
                    {
                        if (ball.SpeedX > -16)
                            ball.SpeedX--;
                    }
                }
            }
            else
            {
                if (_padState.IsButtonDown(Buttons.DPadRight) || _keyState.IsKeyDown(Keys.Right))
                {
                    rec.X += 10;

                }
                else if (_padState.IsButtonDown(Buttons.DPadLeft) || _keyState.IsKeyDown(Keys.Left))
                {
                    rec.X -= 10;

                }
                rec.X = (int)MathHelper.Clamp(rec.X, 0, (_gameWidth - rec.Width));

                if (rec.Intersects(ball.Rec))
                {
                    ball.SpeedY = ball.SpeedY * -1;
                    ball.LastHit = this;
                    if (position == Game1.Position.Top)
                    {
                        if (ball.SpeedY < 16)
                            ball.SpeedY++;

                    }
                    else
                    {
                        if (ball.SpeedY > -16)
                            ball.SpeedY--;
                    }
                }
            }
        }
    }
}
